package cn.swenty.backstage.framework.taglib;

import cn.swenty.backstage.framework.common.util.StringUtil;

public class ShowListTag  extends SimpleTag{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String url;
	private String formatShow;
	private String formatHide;
	private String width;
	private String height;
	private String bindingId;
	private String click;

	protected String getPrintStr() throws Exception {

		StringBuffer sb = new StringBuffer();
		
		//this.addLine(sb, "<link href=\"" + this.webRoot()	+ "/plugin/ztree/css/zTreeStyle/zTreeStyle.css\" rel=\"stylesheet\" type=\"text/css\" />");
		//this.addLine(sb, "<script language='Javascript' src='" + this.webRoot()	+ "/plugin/ztree/js/jquery.ztree.all.min.js'></script>");
		this.addLine(sb,  "<style type=\"text/css\">.showlist p{height:25px;margin:0 0 0 1px;} p:hover {  color: #E27777;background: #D6D6D6; } .showlist p:ACTIVE {  color: #E27777;background: #D6D6D6;} </style>");
		this.addLine(sb, "<script language='javascript'>");
		this.addLine(sb, " var temp_value = '';");
		this.addLine(sb, " function list_p_click(str){");
		if(StringUtil.isNotNullTrim(click)){
			
			this.addLine(sb, " eval('"+click+"(str)')");
		}
		this.addLine(sb, " $('#_div_"+bindingId+"').slideUp('fast'); };");

		this.addLine(sb,
				"$(function(){");
		this.addLine(sb,"$('#"+bindingId+"').keyup(function(){");
		this.addLine(sb, "if(temp_value!=$('#"+bindingId+"').val()){");
		this.addLine(sb, "temp_value = $('#"+bindingId+"').val(); ");
		this.addLine(sb, " if($('#"+bindingId+"').val()!=''){ ");
		this.addLine(sb, "$.ajax({type:'POST',async:false,url:'"+url+"',data:{value_:temp_value},dataType:'json',success:function(jsonDatas){");
		this.addLine(sb,		"$('#_div_"+bindingId+"').html('');");
		this.addLine(sb,	"for(var i =0;i<jsonDatas.length;i++){");
		this.addLine(sb,	" var line = '' ;");
		this.addLine(sb,		" var fs ='"+formatShow+"'");
		this.addLine(sb,		" var fh ='"+formatHide+"'");
		this.addLine(sb,	   "$.each(jsonDatas[i],function(key,value){");
		this.addLine(sb,		" fs = fs.replace(new RegExp(key.toString(),'gm'),value)");
		this.addLine(sb,		" fh = fh.replace(new RegExp(key.toString(),'gm'),value)");
		this.addLine(sb,		" line= '<p style=\"cursor: pointer;\" onclick=\"list_p_click(\\''+fh+'\\')\">'+fs+'</p>'");
		this.addLine(sb,		"})");
		this.addLine(sb,		"$('#_div_"+bindingId+"').append(line);");
		this.addLine(sb,	"}");
		//显示
		this.addLine(sb,	" $('#_div_"+bindingId+"').slideDown('fast');");	
		this.addLine(sb,	"}})");	
		this.addLine(sb,	"}else{");
		this.addLine(sb,	"$('#_div_"+bindingId+"').hide();");
		this.addLine(sb,    "}");	
		this.addLine(sb, "}else if($('#"+bindingId+"').val()==''){$('#_div_"+bindingId+"').hide();}");
		
		this.addLine(sb, "var obj=$('#"+bindingId+"');var objOffset=$('#"+bindingId+"').offset(); /*alert(objOffset.left+'-'+objOffset.top+'-'+obj.outerHeight()+'-'+obj.outerWidth());*/ $('#_div_"+bindingId+"').offset({left:objOffset.left + 'px', top:objOffset.top + obj.outerHeight() + 'px'}).css({width:"+(StringUtil.isNotNullTrim(width)?width:"obj.outerWidth()+'px'")+"});");
		this.addLine(sb,"});$('#"+bindingId+"').dblclick(function(){if(temp_value!=''){$('#_div_"+bindingId+"').slideDown('fast');}})");
		this.addLine(sb,"});");
		
		this.addLine(sb, "</script>");
		this.addLine(sb,
				"<div id=\"_div_"+bindingId+"\" class='showlist' style='display:none; position: absolute;border:1px solid #DFDFDF;z-index:9999;overflow:auto;background:#fff;"+(StringUtil.isNotNullTrim(height)?"height:"+height:"")+"'><ul id=\""+bindingId+"\" ></ul></div>");
		return sb.toString();
	}

	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getFormatShow() {
		return formatShow;
	}

	public void setFormatShow(String formatShow) {
		this.formatShow = formatShow;
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public String getHeight() {
		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public String getBindingId() {
		return bindingId;
	}

	public void setBindingId(String bindingId) {
		this.bindingId = bindingId;
	}

	public String getClick() {
		return click;
	}

	public void setClick(String click) {
		this.click = click;
	}


	public String getFormatHide() {
		return formatHide;
	}


	public void setFormatHide(String formatHide) {
		this.formatHide = formatHide;
	}

	
}
